package com.hnas.eastsquare.service;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.common.util.CommonUtils;
import com.hnas.eastsquare.dao.SediBalanceChangeRecordDao;
import com.hnas.eastsquare.dao.SediToBalanceRecordDao;
import com.hnas.eastsquare.dao.SedimentaryBalanceDao;
import com.hnas.eastsquare.domain.BalanceAddType;
import com.hnas.eastsquare.orm.SediBalanceChangeRecord;
import com.hnas.eastsquare.orm.SediToBalanceRecord;
import com.hnas.eastsquare.orm.SedimentaryBalance;
import com.hnas.eastsquare.util.JedisPoolUtil;

import redis.clients.jedis.Jedis;

@Component("sedimentaryBalanceService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class SedimentaryBalanceServiceImpl implements SedimentaryBalanceService{
	
	private static Log logger = LogFactory.getLog(SedimentaryBalanceServiceImpl.class);

	@Autowired
	private SedimentaryBalanceDao sedimentaryBalanceDao;
	@Autowired
	private SediToBalanceRecordDao sediToBalanceRecordDao; 
	@Autowired
	private SediBalanceChangeRecordDao sediBalanceChangeRecordDao;
	//@Autowired
	//private JedisUtil jedisUtil;
	@Autowired
	private JedisPoolUtil jedisPoolUtil;
	
	@Override
	public SedimentaryBalance saveOrUpdate(SedimentaryBalance vo) throws Exception {
		return this.sedimentaryBalanceDao.saveOrUpdate(vo);
	}

	@Override
	public SedimentaryBalance findSingleResult() {
		return this.sedimentaryBalanceDao.findSingleResult();
	}

	@Override
	public SedimentaryBalance sediToBalance(Long amount) throws Exception {
		SediToBalanceRecord sediToBalanceRecord = new SediToBalanceRecord();
		sediToBalanceRecord.setAddType(BalanceAddType.AUTO);
		sediToBalanceRecord.setBalanceAdd(1000*100L);
		sediToBalanceRecord.setId(CommonUtils.getUUID());
		sediToBalanceRecord.setOperateTime(new Date());
		this.sediToBalanceRecordDao.saveOrUpdate(sediToBalanceRecord);//沉淀池转到余额池
		SedimentaryBalance findSingleResult = this.sedimentaryBalanceDao.findSingleResult();
		Long balance = findSingleResult.getBalance();
		Long sedimentary = findSingleResult.getSedimentary();
		findSingleResult.setBalance(balance+amount);
		findSingleResult.setSedimentary(sedimentary-amount);
		return this.sedimentaryBalanceDao.saveOrUpdate(findSingleResult);
	}

	@Override
	public void failBack(SedimentaryBalance findSingleResult,SediBalanceChangeRecord changeRecord) throws Exception{
		changeRecord.setIsDeleted("0");//标记为已删除
		this.sediBalanceChangeRecordDao.saveOrUpdate(changeRecord);
		this.sedimentaryBalanceDao.saveOrUpdate(findSingleResult);		
	}

	@Override
	public void updateByOrderNo(String orderNo,Long amount){
		Jedis jedis = jedisPoolUtil.getJedis();
		long sedi = 0L;
		long bala = 0L;
		Long sedimentary = 0L;
		try{
			logger.info("交易成功1：笔数："+jedis.get("eastSquareCount")+",减免总金额："+jedis.get("eastSquareAmount")+"沉淀金额："+jedis.get("sedimentary")+"余额："+jedis.get("balance"));
			SediBalanceChangeRecord record = this.sediBalanceChangeRecordDao.findByOrderNo(orderNo);
			SedimentaryBalance sediBalance = this.sedimentaryBalanceDao.findSingleResult();
			if(record != null && sediBalance != null){
				sedi = record.getSedimentaryChange();
				bala = record.getBalanceChange();
				sediBalance.setBalance(sediBalance.getBalance()-record.getBalanceChange());
				sediBalance.setSedimentary(sediBalance.getSedimentary()+record.getSedimentaryChange());
				this.sedimentaryBalanceDao.saveOrUpdate(sediBalance);
				/*sedimentary = jedis.decrBy("sedimentary",amount);
				logger.info("orderNo:"+orderNo+",减sedimentary："+amount);
				if(sedimentary<0){
					jedis.incrBy("sedimentary",amount);
					logger.info("orderNo:"+orderNo+",redis不够减，sedimentary加回来："+amount);
				}else{
					SediToBalanceRecord sediToBalanceRecord = new SediToBalanceRecord();
					sediToBalanceRecord.setAddType(BalanceAddType.AUTO);
					sediToBalanceRecord.setBalanceAdd(1000*100L);
					sediToBalanceRecord.setId(CommonUtils.getUUID());
					sediToBalanceRecord.setOperateTime(new Date());
					this.sediToBalanceRecordDao.saveOrUpdate(sediToBalanceRecord);//沉淀池转到余额池
					SedimentaryBalance findSingleResult = this.sedimentaryBalanceDao.findSingleResult();
					Long balance = findSingleResult.getBalance();
					Long tempSedimentary = findSingleResult.getSedimentary();
					findSingleResult.setBalance(balance+amount);
					findSingleResult.setSedimentary(tempSedimentary-amount);
					this.sedimentaryBalanceDao.saveOrUpdate(findSingleResult);
				}*/
			}
			logger.info("交易成功2：笔数："+jedis.get("eastSquareCount")+",减免总金额："+jedis.get("eastSquareAmount")+"沉淀金额："+jedis.get("sedimentary")+"余额："+jedis.get("balance"));
		}catch(Exception e){
			e.printStackTrace();
			logger.info("前端根据二维码轮训订单修改订单异常,orderNo:"+orderNo+","+e.getMessage());
			Long incrBy = jedis.incrBy("balance",bala);
			Long decrBy = jedis.decrBy("sedimentary",sedi);
			logger.info("更新东方广场订单异常,orderNo:"+orderNo+"，balance加回来："+bala+",sedimentary减回去："+sedi);
			/*if(sedimentary>=0){
				jedis.incrBy("sedimentary",amount);
			}*/
		}finally{
//			jedisUtil.closeJedis(jedis);
			if(jedis != null) jedis.close();
		}
		
	}

}
